﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(USTimelineEvent))]
public class USTimelineEventEditor : USTimelineBaseEditor
{
    public override void AddRightClickOptions(GenericMenu contextMenu, Vector2 mousePosition)
    {
        contextMenu.AddSeparator(string.Empty);
        string str = "Add Event";
        string str2 = str + "/";
        System.Type aBaseClass = typeof(USEventBase);
        foreach (System.Type type2 in USEditorUtility.GetAllSubTypes(aBaseClass))
        {
            if (type2.BaseType == aBaseClass)
            {
                ContextData userData = new ContextData();
                userData.mouseOver = base.TargetTimeline as USTimelineEvent;
                userData.time = USControl.convertEventPanePositionToTime(mousePosition.x);
                string text = string.Empty;
                foreach (Attribute attribute in type2.GetCustomAttributes(true))
                {
                    USequencerEventAttribute attribute2 = attribute as USequencerEventAttribute;
                    if (attribute2 != null)
                    {
                        text = str2 + attribute2.EventPath;
                        break;
                    }
                    text = str2 + "Default/" + type2.ToString();
                }
                userData.typeToCreate = type2;
                contextMenu.AddItem(new GUIContent(text), false, new GenericMenu.MenuFunction2(USTimelineEventEditor.ContextClickAdd), userData);
            }
        }
    }

    private static void ContextClickAdd(object obj)
    {
        if (!(obj is ContextData))
        {
            Debug.LogWarning("Passed data that isn't ContextData to USControlTimeline.ContextClickRemove.");
        }
        else
        {
            ContextData data = obj as ContextData;
            string path = "EmptyPrefab";
            GameObject original = Resources.Load(path, typeof(GameObject)) as GameObject;
            if (original == null)
            {
                Debug.Log("Couldn't find prefab for USControlTimeline.ContextClickAdd, it is possible it has been moved from the resources folder");
            }
            else if (data.mouseOver != null)
            {
                Undo.RegisterSceneUndo("Add Event : " + data.typeToCreate.ToString());
                GameObject obj3 = UnityEngine.Object.Instantiate(original) as GameObject;
                obj3.transform.parent = data.mouseOver.transform;
                obj3.name = data.typeToCreate.ToString();
                string input = "Event";
                System.Type typeToCreate = data.typeToCreate;
                foreach (System.Type type2 in typeToCreate.Assembly.GetTypes())
                {
                    if (type2 == typeToCreate)
                    {
                        foreach (Attribute attribute in type2.GetCustomAttributes(true))
                        {
                            USequencerEventAttribute attribute2 = attribute as USequencerEventAttribute;
                            if (attribute2 != null)
                            {
                                input = attribute2.EventPath;
                            }
                        }
                    }
                }
                int count = input.LastIndexOf("/") + 1;
                if ((count > 0) && (count < input.Length))
                {
                    input = input.Remove(0, count);
                }
                input = Regex.Replace(input, "([a-z])([A-Z])", "$1 $2");
                obj3.name = input;
                USEventBase base2 = obj3.AddComponent(data.typeToCreate) as USEventBase;
                base2.Firetime = data.time;
                Selection.activeTransform = base2.transform;
            }
        }
    }

    public override void GetSelectableObjectUnderPos(ref List<UnityEngine.Object> selectedObjects, Rect selectionArea)
    {
        USTimelineEvent targetTimeline = base.TargetTimeline as USTimelineEvent;
        foreach (USEventBase base2 in targetTimeline.Events)
        {
            if (USEditorUtility.DoRectsOverlap(EditorEventObjectMap.GetKey(base2).MyArea, selectionArea))
            {
                selectedObjects.Add(base2);
            }
        }
    }

    private void OnEnable()
    {
        base.Setup();
        this.myArea.height = 45f;
    }

    public override void OnFloatingContents(Rect thisArea)
    {
        thisArea.height = base.MyArea.height;
        thisArea.x += 13f;
        thisArea.width -= thisArea.x;
        USTimelineBase targetTimeline = base.TargetTimeline as USTimelineBase;
        GUI.Label(thisArea, targetTimeline.name);
    }

    public override void OnTimelineBackground(Rect thisArea)
    {
        thisArea.height = base.MyArea.height;
        GUI.Box(thisArea, string.Empty, base.defaultTimelineBackground);
    }

    public override void OnTimelineContents(Rect thisArea)
    {
        thisArea.height = base.MyArea.height;
        base.myArea = thisArea;
        USTimelineEvent targetTimeline = base.TargetTimeline as USTimelineEvent;
        foreach (USEventBase base2 in targetTimeline.Events)
        {
            USEventBaseEditor key = EditorEventObjectMap.GetKey(base2);
            Rect rect = key.DrawEventInTimeline(base2, thisArea);
            key.MyArea = rect;
            if (rect.Contains(Event.current.mousePosition))
            {
                USControlTimelines.MouseOverObject = base2.transform;
            }
            if ((USControlTimelines.MouseOverObject == base2.transform) && !rect.Contains(Event.current.mousePosition))
            {
                USControlTimelines.MouseOverObject = null;
            }
        }
    }

    public override void ProcessRemoveObject()
    {
        USTimelineEvent targetTimeline = base.TargetTimeline as USTimelineEvent;
        foreach (USEventBase base2 in targetTimeline.Events)
        {
            if (USSelection.IsInUnitySelection(base2))
            {
                Undo.RegisterSceneUndo("Remove From Sequence");
                UnityEngine.Object.DestroyImmediate(base2.gameObject);
            }
        }
    }

    private class ContextData
    {
        public USTimelineEvent mouseOver;
        public float time;
        public System.Type typeToCreate;
    }
}

